﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using GN.Pay.Application.AdminApp.Dto.Input;
using GN.Pay.Application.AdminApp.Dto.Output;
using GN.Pay.Core.Repositories;
using Abp.AutoMapper;
using GN.Pay.Core.Entities;
using GN.Pay.Core.Services;
using GN.Pay.Core.Runtime;

namespace GN.Pay.Application.AdminApp.Impl
{
    /// <summary>
    /// 商户信息应用服务
    /// </summary>
    public class MerchantInfoAppService : AdminAppService, IMerchantInfoAppService
    {
        private readonly IMerchantInfoRepository repository;
        private readonly IGenerateIdentifier generateIdentifier;

        public MerchantInfoAppService(IMerchantInfoRepository repository, 
            IGenerateIdentifier generateIdentifier)
        {
            this.repository = repository;
            this.generateIdentifier = generateIdentifier;
        }       

        /// <summary>
        /// 
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public MerchantInfoOutput Register(MerchantInfoRegisterInput input)
        {
            var entity = input.MapTo<MerchantInfo>();
            entity.MerchantName = entity.MerchantName.Trim();
            if (repository.Count(s => s.MerchantName == entity.MerchantName) > 0)
            {
                throw new Abp.UI.UserFriendlyException("商户名称已被注册");
            }
            entity.State = 1;
            entity.Id = generateIdentifier.GenerateMerchantId(entity.MerchantName, entity.MerchantType);
            repository.Insert(entity);
            return entity.MapTo<MerchantInfoOutput>();
        }
    }
}
